<?php
/**
 * common repository file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/08/20
 * Time: 06:07
 */
 
 namespace HiCommon\Repository;

use HiCommon\Model\Admins;

class AdminsRepository extends BaseRepository
{

    public function base_model(){
        return new \HiCommon\Model\Admins();
    }


//    /**
//     * 获取用户权限
//     *
//     * @param int $nId
//     *
//     * @return \HiCommon\Model\RoleUser[]|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function getUserPermissions(int $nId){
//        return EntrustRoleUser::with('role.role_permission')
//                       ->where('user_id',$nId)->get();
//    }

//    /**
//     * 分页获取全部数据
//     *
//     * @param array $arr_option
//     *
//     * @return mixed
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function get_all($arr_option = [])
//    {
//        return Admins::commonget($arr_option);
//    }

    /**
     * 分页获取数据
     *
     * @param array $arr_search
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_search_list($arr_search = [],$arr_option = [])
    {
        return Admins::where(
        function ($query)
        use($arr_search) {
            if(!empty($arr_search['key_word'])){
                $query->where('username', 'like', '%'.$arr_search['key_word'].'%');
            }
            if(!empty($arr_search['phone'])){
                $query->where('phone', $arr_search['phone']);
            }
        })->commonpage($arr_option);
    }
    
//    /**
//     * 通过where分页获取数据
//     *
//     * @param array $arr_where
//     * @param array $arr_option
//     *
//     * @return mixed
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function get_list_by_where($arr_where,$arr_option = [])
//    {
//        return Admins::where($arr_where)->commonpage($arr_option);
//    }

//    /**
//     * 通过where分页获取一条数据
//     *
//     * @param array $arr_where
//     * @param array $arr_option
//     *
//     * @return mixed
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function get_one_by_where($arr_where,$arr_option = [])
//    {
//        $arr_option['field'] = ['id','type','pass','openid','username','realname','nickname','phone','balance'];
//        $arr_option['with'] = ['shop'];
//        if(empty($arr_option['with'])){
//            $arr_option['with'] = [];
//        }
//        if(empty($arr_option['field'])){
//            $arr_option['field'] = ['*'];
//        }
//        return Admins::with($arr_option['with'])->select($arr_option['field'])->where($arr_where)->first();
//    }

//    /**
//     * 通过where分页获取一条数据
//     *
//     * @param array $arr_where
//     * @param array $arr_option
//     *
//     * @return mixed
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function get_one_by_orwhere($arr_where,$arr_option = [])
//    {
//        $arr_option['field'] = ['id','type','pass','openid','username','realname','nickname','phone','balance'];
//        $arr_option['with'] = ['shop'];
//        if(empty($arr_option['with'])){
//            $arr_option['with'] = [];
//        }
//        if(empty($arr_option['field'])){
//            $arr_option['field'] = ['*'];
//        }
//        return Admins::with($arr_option['with'])->select($arr_option['field'])->orwhere($arr_where)->first();
//    }
    
    /**
     * 根据条件统计数量
     *
     * @param array $arr_where
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_count_by_where($arr_where)
    {
        return Admins::where($arr_where)->count();
    }
    
    /**
     * 根据条件统计总和
     *
     * @param array  $arr_where
     * @param string $s_field
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_sum_by_where($arr_where = [],$s_field = 'mark')
    {
        return Admins::where($arr_where)->sum($s_field);
    }

//    /**
//     * 添加一条数据
//     *
//     * @param array $arr_input
//     *
//     * @return mixed
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function add_one($arr_input)
//    {
//        return Admins::create($arr_input);
//    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one(int $n_id, $arr_option = [])
    {
        return Admins::commonfind($n_id, $arr_option);
    }

    /**
     * 修改一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_one(array $arr_input)
    {
        return Admins::where('id', $arr_input['id'])->update($arr_input);
    }

    /**
     * 根据条件修改数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_by_where(array $arr_where,array $arr_input)
    {
        return Admins::where($arr_where)->update($arr_input);
    }

    /**
     * 删除一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_one(int $n_id)
    {
        return Admins::where('id', $n_id)->commondelete(true);
    }

    /**
     * 修改一条数据状态
     *
     * @param int $n_id
     * @param int $n_state
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_one_state(int $n_id,int $n_state)
    {
        return Admins::where('id',$n_id)->update(['state' =>$n_state]);

    }


    /**
     * 通过where获取一条数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one_by_where($arr_where, $arr_option = [])
    {
        return Admins::where($arr_where)->commonfirst($arr_option);
    }


    /**
     * 字段自增
     *
     * @param int    $n_id
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function increase_num($n_id,$s_field = 'commend_num',$n_step = 1)
    {
        return Admins::where('id',$n_id)->increment($s_field,$n_step);
    }

    /**
     * 字段自减
     *
     * @param int    $n_id
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function decrease_num($n_id,$s_field = 'commend_num',$n_step = 1)
    {
        return Admins::where('id',$n_id)->decrement($s_field,$n_step);
    }


    /**
     * 用户登录
     *
     * @param array $data
     *
     * @return bool
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function login_in(array $data):bool
    {
        $arr_data = [
          'username' => trim((string)$data['username']),
          'password' => trim((string)$data['password'])
        ];
        $userSatuts = admin_guard()->attempt($arr_data);

        return $userSatuts;
    }

//    /**
//     * 用户登录
//     *
//     * @param array $data
//     *
//     * @return bool
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function web_login_in(array $data):bool
//    {
//        $arr_data = [
//          'username' => trim((string)$data['username']),
//          'password' => trim((string)$data['password'])
//        ];
//        $userSatuts = web_guard()->attempt($arr_data);
//
//        return $userSatuts;
//    }

    /**
     * 获取门店数据
     */
    public function get_shop_detail($openid)
    {
        return Admins::with(['shop'])->where(['openid'=>$openid])->first(['id']);
    }

    /**
     * 获取门店数据
     */
    public function get_shop_info($with, $openid)
    {
        return Admins::with($with)->where(['openid'=>$openid])->first(['id']);
    }

    /**
     *  获取用户信息 + 店铺信息
     *
     * @param int $n_id
     *
     * @return \HiCommon\Model\Admins|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function user_shop_info($n_id = 0)
    {
        return Admins::with(['shop'])->select(['id','type','username','realname','nickname','phone','balance'])->find($n_id);
    }

}